package com.xyzq.cgate.common.rule.custom.entity;

import com.alibaba.fastjson.annotation.JSONType;
import com.xyzq.cgate.common.constant.Constants;
import jdk.nashorn.internal.objects.annotations.Constructor;
import lombok.*;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.io.Serializable;
import java.util.Optional;

/**
 * @Description:
 * @author chenchen
 * @date 2019/6/12 8:45
 */
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@JSONType(orders = { "module", "method", "ip", "queryType", "rpcType", "appKey", "signCode"})
public class MatchCondition implements Serializable {

   private String module;

   private String method;

   private String ip ;

   private String queryType;

   private String rpcType;

   private String appKey;

   private String signCode;

   public static MatchCondition transform(final ServerHttpRequest request) {
      final String module = Optional.ofNullable(request.getHeaders().getFirst(Constants.MODULE)).orElse("");
      final String method = Optional.ofNullable(request.getHeaders().getFirst(Constants.METHOD)).orElse("");
      final String ip = Optional.ofNullable(request.getHeaders().getFirst(Constants.IP)).orElse("");
      final String queryType = Optional.ofNullable(request.getHeaders().getFirst(Constants.QUERY_TYPE)).orElse("");
      final String rpcType = Optional.ofNullable(request.getHeaders().getFirst(Constants.RPC_TYPE)).orElse("");
      final String appKey = Optional.ofNullable(request.getHeaders().getFirst(Constants.APP_KEY)).orElse("");
      final String signCode = Optional.ofNullable(request.getHeaders().getFirst(Constants.SIGN_CODE)).orElse("");
      return new MatchCondition(module, method, ip, queryType, rpcType, appKey, signCode);
   }

}
